//
//  FSSelectTagView.m
//  AtCar
//
//  Created by 代佳宏 on 2020/11/27.
//  Copyright © 2020 车一转. All rights reserved.
//

#import "FSSelectTagView.h"

@interface FSSelectTagView()
<UICollectionViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) NSMutableArray *tags;///< 所有的标签数据
@property (nonatomic, strong) NSMutableArray *selectTags;///< 选择的标签数据

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cBottomY;
@property (weak, nonatomic) IBOutlet UICollectionView *tagCollectionView;

@end

@implementation FSSelectTagView

+ (instancetype)initWithTags:(NSArray *)tags withSelectTags:(NSArray *)selectTags {
    FSSelectTagView *tagView = [FSSelectTagView initFromNib];
    tagView.frame = CGRectMake(0, 0, iPhone_Width, iPhone_Height);
    tagView.tags = tags.mutableCopy;
    tagView.selectTags = selectTags.mutableCopy;
    
    return tagView;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tagCollectionView.delegate = self;
    self.tagCollectionView.dataSource = self;
    [self.tagCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    
    self.cBottomY.constant = -271 - (IS_IPHONE_X ? 34 : 0);
    [self layoutIfNeeded];
}
   
- (void)show {
    [self showInView:MyAppWindow];
}

- (void)showInView:(UIView *)view {
    [self.tagCollectionView reloadData];
    
    self.bounds = view.bounds;
    [view addSubview:self];
    self.cBottomY.constant = 0;
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)dismiss {
    self.cBottomY.constant = -271 - (IS_IPHONE_X ? 34 : 0);
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - click
- (IBAction)click_CancelBtn:(UIButton *)sender {
    [self dismiss];
}

- (IBAction)click_SubmitBtn:(UIButton *)sender {
    if (self.selectTagBlock) {
        self.selectTagBlock(self.selectTags);
    }
    [self dismiss];
}

#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.tags.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    
    UIImageView *imageView = [cell viewWithTag:101];
    UILabel *nameLabel = [cell viewWithTag:102];

    if (imageView == nil) {
        imageView = [[UIImageView alloc] init];
        imageView.tag = 101;
        [cell.contentView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell.contentView);
        }];
    }
    if (nameLabel == nil) {
        nameLabel = [[UILabel alloc] init];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:10];
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.tag = 102;
        [cell.contentView addSubview:nameLabel];
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell.contentView);
        }];
    }
    
    id model = self.tags[indexPath.row];
    
    if ([self.selectTags containsObject:model]) {
        imageView.image = BASE_BLUE_IMAGE_4;
        nameLabel.textColor = [UIColor whiteColor];
    }
    else {
        imageView.image = BASE_BOX_IMAGE_4;
        nameLabel.textColor = UIColorFromRGB(0x5AAEF2);
    }
    
    nameLabel.text = @"这是标签";
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.tags[indexPath.row];
    if ([self.selectTags containsObject:model]) {
        [self.selectTags removeObject:model];
    }
    else {
        [self.selectTags addObject:model];
    }
    [collectionView reloadData];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = [@"这是标签" widthForFont:[UIFont systemFontOfSize:10]] + 20;
    if (width > (iPhone_Width - 30)) {
        width = iPhone_Width - 30;
    }
    return CGSizeMake(width, 18);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 10;
}




@end
